<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>设置档期</title>

    <!-- Bootstrap -->
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="css/bootstrap.min.css"  >
    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="css/bootstrap-theme.min.css"  >

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap-table.js"></script>
    <script src="js/locale/bootstrap-table-zh-CN.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="js/bootstrap.min.js"></script>
    <!-- Latest compiled and minified Locales -->
    <script src="js/bootstrap-table-export.js"></script>
    <script src="./js/common.js"></script>
    <script src="js/moment-with-locales.min.js"></script>
    <link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet">
    <script src="js/bootstrap-datetimepicker.min.js"></script>
</head>

<hr>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-2 column">
            <!-- 电影档期信息表格-->
            <table id="movieonshowtable" class="table table-hover">

            </table>
        </div>

        <!-- 添加电影信息表单-->
        <div class="col-md-8 column">
            <form class="form-horizontal" role="form">
                <div class="form-group">
                    <input type="hidden" class="form-control" id="movieOnShowId" />

                    <label  class="col-sm-2 control-label">选择电影</label>
                    <select id="selectmovie" name="moviename">
                        <option  name="choose">请选择</option>
                    </select>
                </div>

                <div class="form-group">
                    <label   class="col-sm-2 control-label">选择影院</label>
                    <select id="selectcinema" name="cinemaname">
                        <option  name="choose">请选择</option>
                    </select>
                </div>

                <div class="form-group">
                    <label  class="col-sm-2 control-label">选择影厅</label>
                    <select id="selecthall" name="hall">
                        <option  name="choose">请选择</option>
                    </select>
                </div>

                <div class="form-group">

                    <label class="col-sm-2 control-label">放映日期：</label>
                    <!--指定 date标记-->
                    <div class='input-group date' id='datetimepicker1'>
                        <input type='text' class="form-control" id="showDate"/>
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">放映时间：</label>
                    <!--指定 date标记-->
                    <div class='input-group date' id='datetimepicker2'>
                        <input type='text' class="form-control" id="showTime"/>
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                </div>

                    <hr>
                <div class="form-group">
                    <label  class="col-sm-2 control-label">影片语言</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="lang" />
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">影片单价</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="price" />
                    </div>
                </div>

                <div class="form-group">

                </div>
            </form>

        </div>
    </div>
</div>
<div class="col-sm-offset-2 col-sm-10">
    <button type="submit" class="btn btn-default" onclick="savemovieinfo()">保存</button>
</div>
<!--<div class="col-sm-offset-2 col-sm-10">-->
    <!--<button type="submit" class="btn btn-default" onclick="ceshi()">查询该档期是否冲突</button>-->
<!--</div>-->

<script>
    //记录影片时长
    var movielength;

    $(function(){

        checkCookie();

        getmoviename("selectmovie");
        getcinemaoption("selectcinema");


        //放映日期选择
        $('#datetimepicker1').datetimepicker({
            format: 'YYYY-MM-DD',
            locale: moment.locale('zh-cn')
        });

        //放映时间
        $('#datetimepicker2').datetimepicker({
            format: 'HH:mm',
            locale: moment.locale('zh-cn')
        });

    });

    //测试档期是否与现有档期冲突
function ceshi() {

    var showDate = $("#showDate").val();
    var showTime = $("#showTime").val();
    var val = showDate+" "+showTime;
    val = val.replace(/-/g,"/");
    var date = new Date(val);
    //date.setMinutes(date.getMinutes()+150);
    console.log(date);
    var movieId = $("#selectmovie").val();
    var cinemaHallId = $("#selecthall").val();
    var data={
        "movieId":movieId,
        "cinemaHallId":cinemaHallId,
        "showDate":showDate
    };

    $.ajax({
        url: SERVER_URL + "/manageback/movie/detail",    //请求的url地址
        dataType: "json",   //返回格式为json
        async: false,//请求是否异步，默认为异步，这也是ajax重要特性
        data:{"id":movieId},
        type: "post",   //请求方式
        success: function (result) {
            console.log(result.data.length);
            movielength = result.data.length;
        },
        complete: function () {

        },
        error: function () {
        }
    });

    $.ajax({
        url: SERVER_URL + "/manageback/movieonshow/selectsameonshow",    //请求的url地址
        dataType: "json",   //返回格式为json
        async: false,//请求是否异步，默认为异步，这也是ajax重要特性
        data:data,
        type: "post",   //请求方式
        success: function (result) {
            console.log(result);
            for(var i = 0;i<result.data.length;i++){

                var str =result.data[i].showDate+" "+result.data[i].showTime;
                str = str.replace(/-/g,"/");
                var datetmp = new Date(str);
                console.log(datetmp)
                if(((date.getTime()>datetmp.getTime())&&((date.getTime()-datetmp.getTime())/1000/60)<movielength)){
                    alert("此档期与现有档期冲突，原因：当天该影厅此时段已有电影放映");
                }else if(((date.getTime()<datetmp.getTime())&&((datetmp.getTime()-date.getTime())/1000/60)<movielength)){
                    alert("此档期与现有档期冲突，原因：当天该影厅此时段已有电影放映");
                }else {

                }

            }
        },
        complete: function () {

        },
        error: function () {
        }
    });
}


    function savemovieinfo() {
        var url;
        var movieId = $("#selectmovie").val();
        var cinemaId = $("#selectcinema").val();
        var cinemaHallId = $("#selecthall").val();
        var showDate = $("#showDate").val();
        var showTime = $("#showTime").val();
        var lang = $("#lang").val();
        var price = $("#price").val();

        //判断档期是否冲突
        var val = showDate+" "+showTime;
        val = val.replace(/-/g,"/");
        var date = new Date(val);

        var data={
            "movieId":movieId,
            "cinemaId":cinemaId,
            "cinemaHallId":cinemaHallId,
            "showDate":showDate,
            "showTime":showTime,
            "lang":lang,
            "price":price

        };

        //获取添加档期的电影的影片时长（分钟）
        $.ajax({
            url: SERVER_URL + "/manageback/movie/detail",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: false,//请求是否异步，默认为异步，这也是ajax重要特性
            data:{"id":movieId},
            type: "post",   //请求方式
            success: function (result) {
                console.log(result.data.length);
                movielength = result.data.length;
            },
            complete: function () {

            },
            error: function () {
            }
        });

        //console.log(movieId)
        if(movieOnShowId==''){
            url = SERVER_URL + "/manageback/movieonshow/add";
        }else{
            url = SERVER_URL + "/manageback/movieonshow/update"
        }


        $.ajax({
            url: SERVER_URL + "/manageback/movieonshow/selectsameonshow",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: false,//请求是否异步，默认为异步，这也是ajax重要特性
            data:data,
            type: "post",   //请求方式
            success: function (result) {
                console.log(result);
                if(result.data.length==0||result.data==null){
                    $.ajax({
                        url: SERVER_URL + "/manageback/movieonshow/add",    //请求的url地址
                        dataType: "json",   //返回格式为json
                        async: false,//请求是否异步，默认为异步，这也是ajax重要特性
                        data:data,
                        type: "post",   //请求方式
                        success: function (result) {
                            console.log(result);
                            alert("添加成功");
                        },
                        complete: function () {

                        },
                        error: function () {
                        }
                    });
                }
                for(var i = 0;i<result.data.length;i++){

                    var str =result.data[i].showDate+" "+result.data[i].showTime;
                    str = str.replace(/-/g,"/");
                    var datetmp = new Date(str);
                    console.log(datetmp);
                    if(((date.getTime()>datetmp.getTime())&&((date.getTime()-datetmp.getTime())/1000/60)<movielength)){
                        alert("此档期与现有档期冲突，原因：当天该影厅"+str+"到"+val+"已有电影放映");
                    }else if(((date.getTime()<datetmp.getTime())&&((datetmp.getTime()-date.getTime())/1000/60)<movielength)){
                        alert("此档期与现有档期冲突，原因：当天该影厅"+val+"到"+str+"已有电影放映");
                    }else {

                        //无冲突则保存档期
                        $.ajax({
                            url: SERVER_URL + "/manageback/movieonshow/add",    //请求的url地址
                            dataType: "json",   //返回格式为json
                            async: false,//请求是否异步，默认为异步，这也是ajax重要特性
                            data:data,
                            type: "post",   //请求方式
                            success: function (result) {
                                console.log(result);
                                alert("添加成功");
                            },
                            complete: function () {

                            },
                            error: function () {
                            }
                        });
                    }

                }
            },
            complete: function () {

            },
            error: function () {
            }
        });

    }

    //设置影片选项
    function getmoviename(tcId) {

        $.ajax({
            url: SERVER_URL + "/manageback/movie/list",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: false,//请求是否异步，默认为异步，这也是ajax重要特性
            data:{"page":0,"size":50},
            type: "post",   //请求方式
            success: function (result) {
                var h="";
                $.each(result.data.list, function(key, value) {//拼接option
                    h += "<option value='" + value.movieId + "'>" + value.title
                        + "</option>";
                });
                $("#"+tcId).append(h);//append 添加进去并展示
                $("#"+tcId).on("change",function(a,b,c){
                    var index = $("#"+tcId+" option:selected")[0].index-1;

                })
            },
            complete: function () {

            },
            error: function () {
            }
        });

    }

    //设置影院选项
    function getcinemaoption(tcId) {

        $.ajax({
            url: SERVER_URL + "/manageback/cinema/list",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: false,//请求是否异步，默认为异步，这也是ajax重要特性
            type: "post",   //请求方式
            success: function (result) {
                var h="";
                $.each(result.data.list, function(key, value) {//拼接option
                    h += "<option value='" + value.cinemaId + "'>" + value.cinemaName
                        + "</option>";
                });
                $("#"+tcId).append(h);//append 添加进去并展示
                $("#"+tcId).on("change",function(a,b,c){
                    //var index = $("#"+tcId+" option:selected")[0].index-1;
                    $("#selecthall").html('');
                    console.log($(this).val());
                    gethalloption("selecthall",$(this).val());
                })
            },
            complete: function () {

            },
            error: function () {
            }
        });

    }


    //设置影厅选项
    function gethalloption(tcId,cinemaId) {

        var data = {
            "cinemaId":cinemaId
        }
        $.ajax({
            url: SERVER_URL + "/manageback/cinemahall/findHallBycId",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: false,//请求是否异步，默认为异步，这也是ajax重要特性
            data:data,
            type: "post",   //请求方式
            success: function (result) {
                var h="";
                $.each(result.data.list, function(key, value) {//拼接option
                    h += "<option value='" + value.cinemaHallId + "'>" + value.hallName
                        + "</option>";
                });
                $("#"+tcId).append(h);//append 添加进去并展示
                $("#"+tcId).on("change",function(a,b,c){
                    var index = $("#"+tcId+" option:selected")[0].index-1;

                })
            },
            complete: function () {

            },
            error: function () {
            }
        });

    }
</script>


</body>
</html>